 //
//  QAIMessageTableViewCell.m
//  Pods
//
//  Created by 范浩云 on 2018/3/13.
//
//


#import "QAIMessageTableViewCell.h"
#import "Masonry.h"
#import "NSBundle+GetImg.h"


#define ContentViewTop  180
#define BannerWidth    275
#define BannerHeight    150
#define HeaderViewBottom     15
#define QStatusBarHeight    (QG_IS_IPHONE_X ? 44.0 : 20.0)
#define QNavBarHeight  (44.0)
#define QCustomNavBarHeight  (QStatusBarHeight + QNavBarHeight)
#define QG_IS_IPHONE_X (QDEVICE_WIDTH == 375.f && QDEVICE_HEIGHT == 812.f)
#define QPHOTO_FRAME_WIDTH   10
#define QDEVICE_HEIGHT      [UIScreen mainScreen].bounds.size.height
#define QDEVICE_WIDTH       [UIScreen mainScreen].bounds.size.width
#define QFONT(Size) [UIFont fontWithName:FONT_NAME size:Size]

@interface QAIMessageTableViewCell ()

@property (nonatomic,strong) UIView * cellContentView;

@property (nonatomic,strong) UIImageView * iconImageView;
@property (nonatomic,strong) UILabel * titleLabel;
@property (nonatomic,strong) UILabel * timeLabel;
@property (nonatomic,strong) UILabel * nameLabel;
@property (nonatomic,strong) UILabel * lineLabel;

@end


@implementation QAIMessageTableViewCell


#pragma mark - ********************** View Lifecycle **********************

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        // 创建cell的subview
        [self initCellContent];
        
        // 注册消息
        [self regNotification];
    }
    
    return self;
}


#pragma mark - ********************** init and config **********************

/**
 创建cell所包含的subviews
 */
- (void)initCellContent
{
    [self.contentView addSubview:self.cellContentView];
    self.cellContentView.backgroundColor = [self YXColorWithHexCode:@"FFFFFF"];
    [self.cellContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.cellContentView addSubview:self.nameLabel];
    [self.cellContentView addSubview:self.titleLabel];
    [self.cellContentView addSubview:self.iconImageView];
    [self.cellContentView addSubview:self.timeLabel];
    [self.cellContentView addSubview:self.lineLabel];
    
}


#pragma mark - ******************************************* 对外方法 *********************************

/**
 设置cell内subview的内容和frame
 */
- (void)setCellContent
{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cellContentView.mas_left).offset(15);
        make.top.equalTo(self.cellContentView.mas_top).offset(10);
        make.height.width.mas_equalTo(@44);
    }];

    CGFloat nameW = QDEVICE_WIDTH-107;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(10);
        make.top.equalTo(self.cellContentView.mas_top).offset(10);
        make.height.mas_equalTo(@15);
        make.width.mas_equalTo(nameW);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(10);
        make.bottom.equalTo(self.cellContentView.mas_bottom).offset(-15);
        make.height.mas_equalTo(@15);
        make.width.mas_equalTo(nameW);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.cellContentView.mas_right).offset(-14);
        make.top.equalTo(self.cellContentView.mas_top).offset(17);
        make.height.mas_equalTo(@15);
        make.width.mas_equalTo(50);
    }];
    
    [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cellContentView.mas_left);
        make.bottom.equalTo(self.cellContentView.mas_bottom).offset(-1);
        make.height.mas_equalTo(1.0);
        make.width.mas_equalTo(QDEVICE_WIDTH);
    }];
    
    [self.cellContentView updateConstraints];
    [self.cellContentView updateConstraintsIfNeeded];
    
    self.iconImageView.image = [NSBundle QAIM_arrowImageWithImgName:@"head"];
}


/**
 获取cell的高度
 
 @return 返回cell的高度
 */
+ (CGFloat)getCellHeight
{
    return 0;
}


#pragma mark - ******************************************* 基类方法(一般发生在重写函数) ****************


#pragma mark - ******************************************* Touch Event ***********************

/**
 TODO: 返回按钮点击事件的响应函数
 
 */
- (void)btnClickBack:(UIButton *)aSender
{
    
}


#pragma mark - ******************************************* 私有方法 **********************************

/**
 TODO: 注册消息
 */
- (void)regNotification
{
    
}


#pragma mark - ******************************************* Net Connection Event ********************

#pragma mark - 请求 demo

- (void)req_url_demo
{
    
}


#pragma mark - ******************************************* Delegate Event **************************

#pragma mark - 代理 demo

- (void)delegate_demo
{
    
}


#pragma mark - ******************************************* Notification Event **********************

#pragma mark - 通知 demo

- (void)notification_demo:(NSNotification *)aNotification
{
    
}


#pragma mark - ******************************************* 属性变量的 Set 和 Get 方法 *****************

-(UIView *)cellContentView
{
    if (!_cellContentView) {
        _cellContentView = [[UIView alloc]init];
    }
    return _cellContentView;
}

-(UIImageView *)iconImageView
{
    if (!_iconImageView) {
        UIImageView *  iconImageView  = [[UIImageView alloc] init];
        iconImageView.layer.cornerRadius = 22;
        iconImageView.clipsToBounds = YES;
        iconImageView.backgroundColor = [UIColor yellowColor];
        _iconImageView = iconImageView;
    }
    return _iconImageView;
}

-(UILabel *)timeLabel
{
    if (!_timeLabel) {
        UILabel*timeLabel=[[UILabel alloc]init];
        timeLabel.text = @"04-08";
        timeLabel.textAlignment=NSTextAlignmentRight;
        timeLabel.font=[UIFont fontWithName:@"PingFangSC-Light" size:12];
        timeLabel.textColor=[self YXColorWithHexCode:@"#97979C"];
        _timeLabel =timeLabel;
    }
    return _timeLabel;
}

-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        UILabel*titleLabel=[[UILabel alloc] init];
        titleLabel.text = @"天气正好";
        titleLabel.textAlignment=NSTextAlignmentLeft;
        titleLabel.font=[UIFont fontWithName:@"PingFangSC-Light" size:14];
        titleLabel.textColor=[self YXColorWithHexCode:@"#97979C"];
        _titleLabel = titleLabel;
    }
    return _titleLabel;
}

-(UILabel *)nameLabel
{
    if (!_nameLabel) {
        UILabel*nameLabel=[[UILabel alloc] init];
        nameLabel.text = @"小明";
        nameLabel.textAlignment=NSTextAlignmentLeft;
        nameLabel.font=[UIFont fontWithName:@"PingFangSC-Light" size:16];
        nameLabel.textColor=[self YXColorWithHexCode:@"#24242C"];
        _nameLabel = nameLabel;
    }
    return _nameLabel;
}


-(UILabel *)lineLabel
{
    if (!_lineLabel) {
        UILabel*lineLabel=[[UILabel alloc] init];
        lineLabel.backgroundColor = [self YXColorWithHexCode:@"#F6F6F6"];
        _lineLabel = lineLabel;
    }
    return _lineLabel;
}

- (UIColor *)YXColorWithHexCode:(NSString *)hex
{
    NSString *cleanString = [hex stringByReplacingOccurrencesOfString:@"#" withString:@""];
    if ([cleanString length] == 3) {
        cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@",
                       [cleanString substringWithRange:NSMakeRange(0, 1)],
                       [cleanString substringWithRange:NSMakeRange(0, 1)],
                       [cleanString substringWithRange:NSMakeRange(1, 1)],
                       [cleanString substringWithRange:NSMakeRange(1, 1)],
                       [cleanString substringWithRange:NSMakeRange(2, 1)],
                       [cleanString substringWithRange:NSMakeRange(2, 1)]];
    }
    
    if ([cleanString length] == 6) {
        cleanString = [cleanString stringByAppendingString:@"ff"];
    }
    
    unsigned int baseValue;
    [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
    
    float red = ((baseValue >> 24) & 0xFF) / 255.0f;
    float green = ((baseValue >> 16) & 0xFF) / 255.0f;
    float blue = ((baseValue >> 8) & 0xFF) / 255.0f;
    float alpha = ((baseValue >> 0) & 0xFF) / 255.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}


@end
